package artid.com.arttogo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.ChoicePayActivity;
import artid.com.arttogo.activity.ExpressActivity;
import artid.com.arttogo.activity.OrderDetActivity;
import artid.com.arttogo.bean.CancelOrder;
import artid.com.arttogo.bean.OrderList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.task.CallbackSky;
import artid.com.arttogo.utils.ToastUtil;

/**
 * 订单列表适配器
 */
public class OrderListAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	CallbackSky cs;
	public List<OrderList> listdata;
	private ImageLoader mImageLoader;

	//	String iconUrl;
	public OrderListAdapter(Context mContext) {
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
		listdata = new ArrayList<OrderList>();
		RequestQueue mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}

	public void setdata(List<OrderList> listdata) {
		this.listdata = listdata;
//		iconUrl = iconurl;
		notifyDataSetChanged();
	}

	public void addlist(List<OrderList> listdata) {
		this.listdata.addAll(listdata);
//		iconUrl = iconurl;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listdata.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_order_list, null);
			vh.mainImg = (ImageView) convertView.findViewById(R.id.order_list_mainimg);
			vh.textname = (TextView) convertView.findViewById(R.id.order_list_name);
			vh.cate = (TextView) convertView.findViewById(R.id.order_list_cate);
			vh.price = (TextView) convertView.findViewById(R.id.order_list_price);
			vh.size = (TextView) convertView.findViewById(R.id.order_list_size);
			vh.buy = (TextView) convertView.findViewById(R.id.order_list_buy);
			vh.num = (TextView) convertView.findViewById(R.id.order_list_num);
			vh.state = (TextView) convertView.findViewById(R.id.order_list_state);
			vh.number = (TextView) convertView.findViewById(R.id.order_list_number);
			vh.orderPice = (TextView) convertView.findViewById(R.id.order_list_orderPice);
			vh.logistics = (TextView) convertView.findViewById(R.id.order_list_logistics);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		final OrderList item = listdata.get(position);
		if (item.getOrder_state().equals("10")) {
			vh.buy.setText("确认付款");
			vh.buy.setTextColor(Color.parseColor("#EE7271"));
			vh.buy.setBackgroundResource(R.drawable.orderlist_btn);
			vh.buy.setVisibility(View.VISIBLE);
			vh.logistics.setVisibility(View.GONE);
		} else if (item.getOrder_state().equals("20")) {
			vh.buy.setText("提醒发货");
			vh.buy.setTextColor(Color.parseColor("#000000"));
			vh.buy.setBackgroundResource(R.drawable.orderinfo_btn);
			vh.buy.setVisibility(View.GONE);
			vh.logistics.setVisibility(View.GONE);
		} else if (item.getOrder_state().equals("30")) {
			vh.buy.setText("确认收货");
			vh.buy.setTextColor(Color.parseColor("#EE7271"));
			vh.buy.setBackgroundResource(R.drawable.orderlist_btn);
			vh.buy.setVisibility(View.VISIBLE);
			vh.logistics.setVisibility(View.VISIBLE);
		} else {
			vh.logistics.setVisibility(View.GONE);
			vh.buy.setVisibility(View.GONE);
		}
		vh.textname.setText(item.getGoods_name());
		vh.cate.setText(item.getGc_name());
		vh.num.setText(item.getOrder_sn());
		if ("10".equals(item.getOrder_state())) {
			vh.state.setText("未支付");
		} else if ("20".equals(item.getOrder_state())) {
			vh.state.setText("未发货");
		} else if ("30".equals(item.getOrder_state())) {
			vh.state.setText("已发货");
		} else if ("40".equals(item.getOrder_state())) {
			vh.state.setText("已完成");
		}else if(item.getOrder_state().equals("0")){
            vh.state.setText("已取消");
        }
		vh.size.setText(item.getGoods_size());
		vh.price.setText(item.getGoods_price());
		vh.orderPice.setText(item.getGoods_price());
		Glide.with(mContext).load(item.getGoods_image() + AppUtil.picWidthUrl)
				.placeholder(R.drawable.ic_menu_rotate)
				.error(R.drawable.ic_menu_rotate).into(vh.mainImg);

		vh.buy.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (item.getOrder_state().equals("10")) {
					//确认付款
					Intent intent0 = new Intent(mContext, ChoicePayActivity.class);
					intent0.putExtra("id", item.getOrder_id());
					mContext.startActivity(intent0);
				} else if (item.getOrder_state().equals("20")) {
					//提醒发货
				} else if (item.getOrder_state().equals("30")) {
					//确认收货
					reqState(item.getOrder_id(), "40", position);
				}
			}
		});
		vh.logistics.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//查看物流信息
				Intent intent = new Intent(mContext, ExpressActivity.class);
				intent.putExtra("id", item.getOrder_id());
				intent.putExtra("orderNum", item.getOrder_sn());
				mContext.startActivity(intent);
			}
		});


		return convertView;
	}


	class ViewHolder {
		TextView textname, cate, size, price, num, state, number, orderPice, buy, logistics;
		ImageView mainImg;

	}

	void reqState(String orderId, final String state, final int index) {

		VolleyBasic.getInstance((Activity) mContext).post(Cache.cancelOrder,
				VolleyMap.getInstance().cancelOrder(orderId, state), CancelOrder.class,
				new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						CancelOrder itemCancelOrder = (CancelOrder) vd.getData();

						if (itemCancelOrder.getState().equals("1")) {
							    //if (state.equals("30")){
                                    OrderList item = listdata.get(index);
                                    item.setOrder_state("40");
                                    listdata.set(index, item);
                                    notifyDataSetChanged();
                                //}
						}
						return;
						//}
						//ArtistInfo list = (ArtistInfo)vd.getData();
					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub

					}
				});
	}

}
